
package com.oahcfly.chgame.core.actions;

import com.badlogic.gdx.math.Bezier;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.actions.ParallelAction;

/** <pre>
 * 封装各类Action：
 * 旋转+掉落并发效果，滑动回弹效果，中心缩放效果。上下跳动，左右跳动。
 * date: 2015-1-4
 * </pre>
 * 
 * @author caohao */
public class CHActions {

	public static AlphaAction createFadeInAction (float duration) {
		return Actions.fadeIn(duration);
	}

	public static AlphaAction createFadeOutAction (float duration) {
		return Actions.fadeOut(duration);
	}

	/** <pre>
	 * 垂直跳动
	 * 
	 * date: 2015-4-19
	 * </pre>
	 * 
	 * @author caohao
	 * @param duration
	 * @return */
	public static Action createShakeVerticalForeverAction (float duration) {
		Action repeatedAction = Actions.sequence(Actions.moveBy(0, 6, duration), Actions.moveBy(0, -6, duration),
			Actions.moveBy(0, 3, duration), Actions.moveBy(0, -3, duration));
		return Actions.forever(repeatedAction);
	}

	/** <pre>
	 * 水平晃动
	 * 
	 * date: 2015-4-19
	 * </pre>
	 * 
	 * @author caohao
	 * @param duration
	 * @return */
	public static Action createShakeHorizontalForeverAction (float duration) {
		Action repeatedAction = Actions.sequence(Actions.moveBy(6, 0, duration), Actions.moveBy(-6, 0, duration),
			Actions.moveBy(3, 0, duration), Actions.moveBy(-3, 0, duration));
		return Actions.forever(repeatedAction);
	}

	/** <pre>
	 * 上下跳动
	 * 
	 * date: 2015-1-19
	 * </pre>
	 * 
	 * @author caohao
	 * @param distance 跳的高度
	 * @param duration 花费时长
	 * @param repeatCount 重复次数
	 * @return */
	public static Action createUpAndDownAction (int distance, float duration, int repeatCount) {
		Action actionUp = Actions.moveBy(0, distance, duration);
		Action action = Actions.moveBy(0, -distance, duration);
		Action actionDown = Actions.moveBy(0, -distance, duration);
		Action action2 = Actions.moveBy(0, distance, duration);
		return Actions.repeat(repeatCount, Actions.sequence(actionUp, action, actionDown, action2));
	}

	/** <pre>
	 * 左右晃动
	 * 
	 * date: 2015-1-19
	 * </pre>
	 * 
	 * @author caohao
	 * @param distance 晃动的宽度
	 * @param duration 花费时长
	 * @param repeatCount 重复次数
	 * @return */
	public static Action createLeftAndRightAction (int distance, float duration, int repeatCount) {
		Action actionLeft = Actions.moveBy(-distance, 0, duration);
		Action action = Actions.moveBy(distance, 0, duration);
		Action actionRight = Actions.moveBy(distance, 0, duration);
		Action action2 = Actions.moveBy(-distance, 0, duration);
		return Actions.repeat(repeatCount, Actions.sequence(actionLeft, action, actionRight, action2));
	}

	/** <pre>
	 * 旋转并按照抛物线移动某段距离
	 * 【起始点为抛物线的顶点】
	 * date: 2015-1-5
	 * </pre>
	 * 
	 * @author caohao
	 * @param duration 时长
	 * @param rotateAmount 旋转次数
	 * @param moveXAmount X方向移动距离
	 * @param moveYAmount Y方向移动距离
	 * @return */
	public static Action createRotateAndMoveAction (float duration, int rotateAmount, int moveXAmount, int moveYAmount) {
		Action rotateAction = Actions.rotateTo(360 * rotateAmount, duration);
		CHParabolaAction parabolaAction = new CHParabolaAction();
		parabolaAction.setAmount(moveXAmount, moveYAmount);
		parabolaAction.setDuration(duration);
		ParallelAction parallelAction = new ParallelAction(rotateAction, parabolaAction);
		return parallelAction;
	}

	/** <pre>
	 * 自动缩放
	 * 
	 * date: 2015-1-7
	 * </pre>
	 * 
	 * @author caohao
	 * @param targetScale 目标缩放倍数 如：缩小=0.8f,放大=1.2f
	 * @param singleScaleDuration 缩放到目标倍数耗时（单位:s）
	 * @param repeatCount -1=无限次执行，1=执行1次
	 * @return */
	public static Action createAutoScaleToCenter (float targetScale, float singleScaleDuration, int repeatCount) {
		Action repeatedAction = Actions.sequence(Actions.scaleTo(targetScale, targetScale, singleScaleDuration),
			Actions.scaleTo(1f, 1f, singleScaleDuration));
		Action action = Actions.repeat(repeatCount, repeatedAction);
		return action;
	}

	/** <pre>
	 * 回弹效果【仅限横向】
	 * 
	 * date: 2015-1-7
	 * </pre>
	 * 
	 * @author caohao
	 * @param targetX 目标X
	 * @param targetY 目标Y
	 * @param duration 时长
	 * @param backDistance 回弹距离
	 * @return */
	public static Action createMoveBackAction (float targetX, float targetY, float duration, int backDistance) {
		Action passAction = Actions.moveTo(targetX + backDistance, targetY, duration);
		Action tmpAction = Actions.moveTo(targetX, targetY, duration);
		Action backAction = Actions.moveTo(targetX - backDistance, targetY, duration);
		Action action = Actions.sequence(passAction, tmpAction, backAction, Actions.moveTo(targetX, targetY, 0.1f));
		return action;
	}

	/** <pre>
	 * 闪烁效果
	 * 
	 * date: 2015-1-7
	 * </pre>
	 * 
	 * @author caohao
	 * @param duration 单次闪烁时长[不可见->可见]
	 * @param repeatCount 闪烁次数
	 * @return */
	public static Action createAutoTwinkleAction (float duration, int repeatCount) {
		Action action = Actions.repeat(repeatCount, Actions.sequence(Actions.fadeOut(duration / 2), Actions.fadeIn(duration / 2)));
		return action;
	}

	/**
	 * 
	 * 贝塞尔曲线运动
	 * @param duration 时长
	 * @param bezier 贝塞尔参数
	 * @return
	 */
	public static CHBezierToAction createBezierToAction (float duration, Bezier<Vector2> bezier) {
		CHBezierToAction bezierToAction = Actions.action(CHBezierToAction.class);
		bezierToAction.setBezier(bezier);
		bezierToAction.setDuration(duration);
		return bezierToAction;
	}
}
